package net.pinnoocle.playweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.aop.PassToken;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.PayInfo;
import net.pinnoocle.playdao.entity.SimpleUserInfoVo;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.shopPc.coupon.CouponListPageVo;
import net.pinnoocle.playdao.entity.shopPc.coupon.CouponListSearchParams;
import net.pinnoocle.playdao.entity.web.master.MasterGroupListVo;
import net.pinnoocle.playdao.entity.web.order.UnPayOrderVo;
import net.pinnoocle.playdao.entity.web.user.*;
import net.pinnoocle.playservice.service.CommonService;
import net.pinnoocle.playservice.service.PayService;
import net.pinnoocle.playservice.service.impl.CouponServiceImpl;
import net.pinnoocle.playservice.service.impl.GroupServiceImpl;
import net.pinnoocle.playservice.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author ye
 * @Description
 * @create 2021-10-25 17:10
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关")
@Slf4j
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    GroupServiceImpl groupService;
    @Autowired
    CommonService commonService;
    @Autowired
    PayService payService;
    @Autowired
    CouponServiceImpl couponService;

    @PostMapping("decodeWechatPhone")
    @ApiOperation("解密微信手机号")
    public BaseResult<String> decodeWechatPhone(@RequestBody WechatEncryptData data){
        return new BaseResult<String>().ok(userService.encryptWechatPhone(data));
    }

    @GetMapping("userCenter")
    @ApiOperation("用户个人中心")
    public BaseResult<UserCenterPageVo> userCenter(){
        UserCenterPageVo userCenterPageVo = userService.centerPage();
        return new BaseResult<UserCenterPageVo>().ok(userCenterPageVo);
    }

    @GetMapping("joinGroup")
    @ApiOperation("用户加入组局-定金下单")
    public BaseResult<String> joinGroup(@RequestParam("groupId") String groupId){
        String orderId = groupService.userJoinGroup(groupId);
        return new BaseResult<String>().ok(orderId);
    }

    @PostMapping("applyMaster")
    @ApiOperation("申请成为DM")
    public BaseResult<String> applyMaster(@Validated @RequestBody ApplyMasterParams params){
        boolean result = userService.applyMaster(params);
        if(result){
            return new BaseResult<String>().success("申请DM成功");
        }
        return new BaseResult<String>().error("申请DM失败");
    }

    @GetMapping("appointAmountPay")
    @ApiOperation("定金去支付")
    public BaseResult<PayInfo> appointAmountPay(@RequestParam("orderId") String orderId) throws Exception {
        return new BaseResult<PayInfo>().ok(userService.appointAmountPay(orderId));
    }

    @GetMapping("appointOrderQuery")
    @ApiOperation("定金订单支付状态查询")
    public BaseResult<String> appointOrderQuery(@RequestParam("orderId") String orderId) throws Exception {
        String payStatus = userService.appointOrderQuery(orderId);
        if(Objects.equals(payStatus, "success")){
            return new BaseResult<String>().success("success");
        }
        return new BaseResult<String>().error("fail");
    }

    @GetMapping("unPayOrder")
    @ApiOperation("未支付订单回显")
    public BaseResult<UnPayOrderVo> unPayOrder(){
        return new BaseResult<UnPayOrderVo>().ok(userService.unPayOrder());
    }

    @GetMapping("unPayOrderGoPay")
    @ApiOperation("未支付订单重新发起支付")
    public BaseResult<PayInfo> unPayOrderGoPay(@RequestParam("orderId") String orderId) throws Exception {
        return new BaseResult<PayInfo>().ok(userService.unPayOrderGoPay(orderId));
    }

    @GetMapping("couponList")
    @ApiOperation("待领取的优惠券列表")
    public BaseResult<PageModel<CouponListPageVo>> couponList(CouponListSearchParams params){
        return new BaseResult<PageModel<CouponListPageVo>>().ok(couponService.couponList(params));
    }

    @GetMapping("userCouponList")
    @ApiOperation("用户优惠券列表")
    public BaseResult<PageModel<UserCouponListPageVo>> userCouponList(UserCouponListSearchParams params){
        return new BaseResult<PageModel<UserCouponListPageVo>>().ok(couponService.userCouponList(params));
    }

    @GetMapping("receiveCoupon")
    @ApiOperation("领取优惠券")
    public BaseResult<String> receiveCoupon(@RequestParam("couponId") String couponId){
        boolean result = couponService.receiveCoupon(couponId);
        if(result){
            return new BaseResult<String>().success("领取成功");
        }
        return new BaseResult<String>().error("领取失败");
    }

    @GetMapping("collectScript")
    @ApiOperation("收藏剧本")
    public BaseResult<String> collectScript(@RequestParam("scriptId") String scriptId){
        String message = userService.collectScript(scriptId);
        if(StringUtils.isNotBlank(message)){
            return new BaseResult<String>().success(message);
        }
        return new BaseResult<String>().error("操作失败");
    }

    @GetMapping("playScript")
    @ApiOperation("我玩过的本点击")
    public BaseResult<String> playScript(@RequestParam("scriptId") String scriptId){
        boolean result = userService.playScript(scriptId);
        if(result){
            return new BaseResult<String>().success("操作成功");
        }
        return new BaseResult<String>().error("操作失败");
    }

    @GetMapping("topUserList")
    @ApiOperation("用户排行榜")
    @PassToken
    public BaseResult<PageModel<SimpleUserInfoVo>> topUserList(@RequestParam("page") Integer page,
                                                               @RequestParam("pageSize") Integer pageSize){
        return new BaseResult<PageModel<SimpleUserInfoVo>>().ok(userService.topUserList(page,pageSize));
    }

    @GetMapping("userScriptDetail")
    @ApiOperation("用户关于剧本的详情")
    public BaseResult<UserDetailVo> userScriptDetail(@RequestParam("scriptId") String scriptId){
        return new BaseResult<UserDetailVo>().ok(userService.userScriptDetail(scriptId));
    }

    @GetMapping("myGroupList")
    @ApiOperation("我的组局")
    public BaseResult<PageModel<MasterGroupListVo>> myGroupList(MyGroupListSearchParams params){
        return new BaseResult<PageModel<MasterGroupListVo>>().ok(userService.myGroupList(params));
    }
}
